//
//  KHHTravelNotesDetailLocationPageTableViewCell.m
//  Travel
//
//  Created by dllo on 15/12/1.
//  Copyright © 2015年 doll. All rights reserved.
//

#import "KHHTravelNotesDetailLocationPageTableViewCell.h"
#import "KHHTravelNotesDetailLocationPageCollectionViewCell.h"
#import "UIImageView+WebCache.h"


@interface KHHTravelNotesDetailLocationPageTableViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)UIImageView *photo;
@property (nonatomic, retain)UILabel *userNameLabel;
@property (nonatomic, retain)UILabel *titleLabel;//文章标题
@property (nonatomic, retain)UILabel *subTitleLabel; //副标题
@property (nonatomic, retain)UICollectionView *collectionView; //装图片
@property (nonatomic, retain)UICollectionViewFlowLayout *layout;
@property (nonatomic, retain)KHHRecommendModel *modelWaypoints;

@end


@implementation KHHTravelNotesDetailLocationPageTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView
{
    self.photo = [[UIImageView alloc]init];
    [self.contentView addSubview:self.photo];
   
    self.userNameLabel = [[UILabel alloc ]init];
    self.userNameLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:self.userNameLabel];
    
    self.titleLabel = [[UILabel alloc]init];
    [self.contentView addSubview:self.titleLabel];
    self.titleLabel.textColor = [UIColor colorWithRed:0.36 green:0.75 blue:0.8 alpha:1];
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    
    self.subTitleLabel = [[UILabel alloc]init];
    [self.contentView addSubview:self.subTitleLabel];
    self.subTitleLabel.numberOfLines = 5;
    self.subTitleLabel.font = [UIFont systemFontOfSize:14];
    
    
    self.layout = [[UICollectionViewFlowLayout alloc]init];
    // self.layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.layout.minimumInteritemSpacing = 10;
    self.layout.minimumLineSpacing = 10;
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.layout] ;
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.93 alpha:1];
    [self.contentView addSubview:self.collectionView];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    //注册cell
    [self.collectionView registerClass:[KHHTravelNotesDetailLocationPageCollectionViewCell class] forCellWithReuseIdentifier:@"itemLocation"];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
//    //夜间模式
//    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"isNight"]) {
//        self.contentView.backgroundColor = [UIColor colorWithRed:0.28 green:0.28 blue:0.28 alpha:1];
//        self.collectionView.backgroundColor = [UIColor colorWithRed:0.28 green:0.28 blue:0.28 alpha:1];
//        self.userNameLabel.textColor = [UIColor whiteColor];
//        self.subTitleLabel.textColor = [UIColor whiteColor];
//        self.titleLabel.textColor = [UIColor whiteColor];
//        
//    }else
//    {
//        self.contentView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
//    }
    
    self.photo.frame = CGRectMake(10, 20, HHWIDTH / 9, HHWIDTH / 9);
    self.photo.layer.masksToBounds = YES;
    self.photo.layer.cornerRadius = HHWIDTH / 18;
    self.userNameLabel.frame = CGRectMake(self.photo.frame.size.width + 25, self.photo.frame.origin.y + 8, HHWIDTH - 10 - self.photo.frame.size.width - 20 - 10 , 10);
    self.titleLabel.frame = CGRectMake(self.userNameLabel.frame.origin.x, self.userNameLabel.frame.origin.y + 15 + 5,self.userNameLabel.frame.size.width , 10);
    
    self.layout.itemSize = CGSizeMake( 200, 150);
    
    CGFloat hSubtitle = [[self class] heightWithText:self.modelTrips.trip_text fontSize:14 Width:HHWIDTH - 20];
    self.subTitleLabel.frame = CGRectMake(self.photo.frame.origin.x,self.photo.frame.origin.y +self.photo.frame.size.height + 15 , HHWIDTH - 20, hSubtitle);
    
    if (self.modelTrips.trip_text.length != 0) {
        self.collectionView.frame  = CGRectMake(10, self.subTitleLabel.frame.origin.y + self.subTitleLabel.frame.size.height + 10, HHWIDTH - 20, 150);
        
    } else {
        self.collectionView.frame  = CGRectMake(10, self.photo.frame.origin.y + self.photo.frame.size.height + 15, HHWIDTH - 20, 150);
    }
}

- (void)setModelAllDate:(KHHRecommendModel *)modelAllDate
{
    
}

- (void)setModelTrip:(KHHRecommendModel *)modelTrip
{
    _modelTrip = modelTrip;
    
    self.titleLabel.text = [NSString stringWithFormat:@"%s %@","@",modelTrip.name];
    self.subTitleLabel.text = modelTrip.trip_text;
}

- (void)setModelUser:(KHHRecommendModel *)modelUser
{
    
    _modelUser = modelUser;
    
    [self.photo sd_setImageWithURL:[NSURL URLWithString:modelUser.avatar_l] placeholderImage:nil];
    self.userNameLabel.text = modelUser.name;
}

- (void)setModelTrips:(KHHRecommendModel *)modelTrips
{
    _modelTrips = modelTrips;

    //判断副标题是否有内容
    if (self.modelTrips.trip_text.length != 0) {
        //cell重用过后如果label被移除  重新创建一次
        if (self.subTitleLabel == nil) {
            
            self.subTitleLabel = [[UILabel alloc]init];
            [self.contentView addSubview:self.subTitleLabel];
            self.subTitleLabel.numberOfLines = 5;
            self.subTitleLabel.font = [UIFont systemFontOfSize:14];
        }
        self.subTitleLabel.text = modelTrips.trip_text;
        
    } else {
        
        //移除父视图
        [self.subTitleLabel removeFromSuperview];
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.waypointsArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    KHHTravelNotesDetailLocationPageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"itemLocation" forIndexPath:indexPath];
    
    self.modelWaypoints = self.waypointsArr[indexPath.item];
    cell.modelWaypoints = self.waypointsArr[indexPath.item];
    cell.backgroundColor = [UIColor redColor];
    return cell;
}


//计算文本高度的方法
+ (CGFloat)heightWithText:(NSString *)text fontSize:(CGFloat)fontsize Width:(CGFloat)width

{
    //设置一个字典 保存文本属性
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontsize]};
    
    //预设尺寸 文本最大不会超过这个范围
    CGSize size = CGSizeMake(width, 2000);
    //根据文本信息 计算得到一个CGRect
    //参数1. 尺寸范围
    //参数2. 获取文本尺寸的方式
    //参数3. 文本属性
    //参数4. 无用信息(上下文)
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}





- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
